package com.av.android;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

public class SerachOptionsActivity extends BaseActivity {

	Spinner tagSpinner;
	Spinner disabilitySpinner;
	Spinner statusSpinner;
	List<Tag> tags;
	String selectedTag ;
	String selectedDisability;
	String selectedStatus; 
	SearchActivity2 parent;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_serach_options);
		tagSpinner = (Spinner) findViewById(R.id.tag_spinner);
		disabilitySpinner = (Spinner) findViewById(R.id.disability_spinner);
		statusSpinner = (Spinner) findViewById(R.id.status_spinner);
		parent = (SearchActivity2)getParent();
		
		new Thread(new Runnable() {
			
			public void run() {
				showLoading("");
				// TODO Auto-generated method stub
				String response =  Utils.httpGet("GetTags");
				ObjectMapper mapper = new ObjectMapper();
				try {
					tags = mapper.readValue(response,
							new TypeReference<List<Tag>>() {
					});
					tags.add(0,new Tag(-1, "Select One"));

				} catch (Exception e) {
					e.printStackTrace();
					tags = new ArrayList<Tag>();
					tags.add(new Tag(-1, "Select One"));
				}
				runOnUiThread(new Runnable() {
					
					public void run() {
						hideLoading();
						// TODO Auto-generated method stub
						tagSpinner.setAdapter(new ArrayAdapter<Tag>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, tags));
					}
				});
			}
		}).start();
	}
	public void search(View v){
		selectedTag = ((Tag)tagSpinner.getSelectedItem()).toString();
		selectedDisability = disabilitySpinner.getSelectedItem().toString().replaceAll(" ","");
		selectedStatus = statusSpinner.getSelectedItem().toString().replaceAll(" ", "");
		if(selectedStatus.equals("SelectOne"))
			selectedStatus="null";
		if(selectedDisability.equals("SelectOne"))
			selectedDisability="null";
		if(selectedTag.equals("Select One"))
			selectedTag = "null";
		showLoading("");
		new Thread(new Runnable() {
			
			public void run() {
				final List<Report> reports = Utils.searchReports(selectedTag,selectedDisability,selectedStatus);
				runOnUiThread(new Runnable() {
					
					public void run() {
						// TODO Auto-generated method stub
						hideLoading();
						parent.refreshResults(reports);
						tagSpinner.setSelection(0);
						statusSpinner.setSelection(0);
						disabilitySpinner.setSelection(0);
					}
				});
			}
		}).start();
		
		
	}


}
